package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.CoachBookingRecord;
import com.newway.back.mapper.CoachBookingRecordMapper;
import com.newway.common.context.UserInfo;
import com.newway.common.context.UserInfoContext;
import com.newway.common.mapper.UserInfoMapper;
import com.newway.common.result.Result;
import com.newway.wechat.dto.PlaceBookingRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/coachBooking")
public class CoachBookingRecordController {

    @Resource
    CoachBookingRecordMapper coachBookingRecordMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @PostMapping("/addOrUpdate")
    public Result add(@RequestBody CoachBookingRecord coachBookingRecord){
        if(coachBookingRecord.getId()==null){
            coachBookingRecord.setUserId(UserInfoContext.getUserInfo().getId());
            coachBookingRecord.setStatus("0");
            coachBookingRecord.setCreateTime(LocalDateTime.now());
            coachBookingRecordMapper.insert(coachBookingRecord);
        }else{
            coachBookingRecordMapper.updateById(coachBookingRecord);
        }
        return Result.success();
    }

    @GetMapping("/bookingRecords")
    public Result list(CoachBookingRecord coachBookingRecord){
        Page<CoachBookingRecord> coachBookingRecordPage = new Page<>(coachBookingRecord.getPageNum(),coachBookingRecord.getPageSize());
        QueryWrapper<CoachBookingRecord> queryWrapper = new QueryWrapper<>();
        if("wechat".equals(UserInfoContext.getUserInfo().getUserFrom())){
            queryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        }
        if(StringUtils.isNotBlank(coachBookingRecord.getStatus())){
            queryWrapper.eq("status",coachBookingRecord.getStatus());
        }
        queryWrapper.orderByDesc("create_time");
        Page<CoachBookingRecord> result = coachBookingRecordMapper.selectPage(coachBookingRecordPage, queryWrapper);
        return Result.success(result);
    }

    @PostMapping("/cancelBooking/{id}")
    public Result cancelBooking(@PathVariable("id")Integer id){
        CoachBookingRecord coachBookingRecord = coachBookingRecordMapper.selectById(id);
        if("0".equals(coachBookingRecord.getStatus())){
            coachBookingRecord.setStatus("1");
            coachBookingRecordMapper.updateById(coachBookingRecord);
            return Result.success();
        }
        return Result.fail("状态已变更，请刷新后查看");
    }

    @PostMapping("/details/{id}")
    public Result details(@PathVariable("id")Integer id){
        CoachBookingRecord coachBookingRecord = coachBookingRecordMapper.selectById(id);
        UserInfo userInfo = userInfoMapper.selectById(coachBookingRecord.getUserId());
        coachBookingRecord.setUserName(userInfo.getUserName());
        return Result.success(coachBookingRecord);
    }

    @PostMapping("/approval/{id}/{status}")
    public Result approval(@PathVariable("id")Integer id,@PathVariable("status")String status){
        CoachBookingRecord coachBookingRecord = coachBookingRecordMapper.selectById(id);
        if("0".equals(coachBookingRecord.getStatus())){
            coachBookingRecord.setStatus(status);
            coachBookingRecordMapper.updateById(coachBookingRecord);
            return Result.success();
        }
        return Result.fail("状态已变更，请刷新后查看");
    }

}
